package com.xd.core.business.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xd.common.constant.SysConstant;
import com.xd.common.exception.XkdException;
import com.xd.common.util.SecurityUtil;
import com.xd.common.util.XkdUtil;
import com.xd.core.business.dto.*;
import com.xd.core.business.entity.Information;
import com.xd.core.business.entity.UsePrint;
import com.xd.core.business.mapper.InformationMapper;
import com.xd.core.business.mapper.UsePrintMapper;
import com.xd.core.business.service.IUsePrintService;
import com.xd.core.business.vo.UsePrintPageVo;
import com.xd.core.business.vo.UsePrintVo;
import com.xd.core.system.service.ISysUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 用印表 服务实现类
 * </p>
 *
 * @author zch
 * @since 2025-06-23
 */
@Service
public class UsePrintServiceImpl extends ServiceImpl<UsePrintMapper, UsePrint> implements IUsePrintService {

    @Autowired
    private UsePrintMapper usePrintMapper;
    @Autowired
    private ISysUserService iSysUserService;
    @Autowired
    private InformationMapper informationMapper;

    /***
     * @description 用印申请列表分页查询
     * @author Zch
     * @date 18:17 2025/6/23 星期一
     * @return com.github.pagehelper.Page<com.xd.core.business.vo.UsePrintPageVo>
     **/
    @Override
    public Page<UsePrintPageVo> usePrintList(UsePrintQueryDto dto) {
        String companyType = SecurityUtil.getLoginUser().getUser().getCompanyType();
        Integer companyId = null;
        List<Integer> userIds = null;
        if (companyType.equals(SysConstant.USER_COMPANY_TYPE_ONE) || companyType.equals(SysConstant.USER_COMPANY_TYPE_FOUR)) {
            companyId = dto.getCompanyId();
        } else if (companyType.equals(SysConstant.USER_COMPANY_TYPE_TOW)) {
            companyId = SecurityUtil.getLoginCompanyId();
            userIds = iSysUserService.getUserIdsByGroup();
        } else if (companyType.equals(SysConstant.USER_COMPANY_TYPE_THREE)) {//代理
            return null;
        }
        //开始分页
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        return usePrintMapper.usePrintList(dto, companyId, userIds);
    }

    /***
     * @description 查看用印
     * @author Zch
     * @date 19:55 2025/6/23 星期一
     * @return com.xd.core.business.vo.UsePrintVo
     **/
    @Override
    public UsePrintVo getUsePrint(Integer usePrintId, String uid, String typeOfSealUsed) {
        if (StrUtil.isEmpty(usePrintId.toString()) || StrUtil.isEmpty(uid))
            throw new RuntimeException("参数错误");
        UsePrintVo print = usePrintMapper.getUsePrint(usePrintId);
        if (ObjectUtil.isEmpty(print))
            throw new RuntimeException("暂无用印数据");
        //获取用印资料
        List<Information> list = informationMapper.selectInformationList(uid, typeOfSealUsed);
        print.setInformation(list);
        return print;
    }

    /***
     * @description 用印审批
     * @author Zch
     * @date 11:25 2025/6/24 星期二
     * @return void
     **/
    @Override
    public void checkUsePrint(CheckUsePrintDto dto) {
        XkdUtil.checkNonNullProperties(dto, "usePrintId", "printingStatus");
        UsePrint usePrint = this.getById(dto.getUsePrintId());
        if (ObjectUtil.isEmpty(usePrint))
            throw new RuntimeException("用印信息不存在");
        usePrint.setPrintingStatus(dto.getPrintingStatus());
        if (dto.getPrintingStatus().equals("2") && ObjectUtil.isNotEmpty(dto.getRejectReason())) {
            usePrint.setRejectReason(dto.getRejectReason());
        }
        this.updateById(usePrint);
    }

    /***
     * @description 用印加急
     * @author Zch
     * @date 11:51 2025/6/24 星期二
     * @return void
     **/
    @Override
    public void usePrintUrgent(UsePrintUrgentDto dto) {
        XkdUtil.checkNonNullProperties(dto, "usePrintId", "isUrgent");
        UsePrint usePrint = this.getById(dto.getUsePrintId());
        if (ObjectUtil.isEmpty(usePrint))
            throw new RuntimeException("用印信息不存在");
        usePrint.setIsUrgent(dto.getIsUrgent());
        this.updateById(usePrint);
    }

    /***
     * @description 用印申请
     * @author Zch
     * @date 10:44 2025/7/1 星期二
     * @return void
     **/
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer addPrintUrgent(AddPrintUrgentDto dto) {
        UsePrint print = new UsePrint();
        if (ObjectUtil.isEmpty(dto))
            throw new RuntimeException("参数错误");
        if("0".equals(dto.getUsePrintInfoDto().getTypeOfSealUsed()) || "1".equals(dto.getUsePrintInfoDto().getTypeOfSealUsed())){
            //判断是否有同用印类型的数据
            LambdaQueryWrapper<UsePrint> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(UsePrint::getIncomingId, dto.getIncomingId())
                    .eq(UsePrint::getTypeOfSealUsed, dto.getUsePrintInfoDto().getTypeOfSealUsed());
            UsePrint usePrint = usePrintMapper.selectOne(wrapper);
            if (ObjectUtil.isNotEmpty(usePrint))
                throw new XkdException("该用印类型申请已存在,请勿重复提交");
        }
        //插入用印信息
        BeanUtils.copyProperties(dto.getUsePrintInfoDto(), print);
        print.setApplyTime(new Date())
                .setIncomingId(dto.getIncomingId())
                .setUid(dto.getUid())
                .setCompanyId(dto.getCompanyId());
        usePrintMapper.insert(print);
        return print.getId();
    }

    /***
     * @description 修改重新申请用印状态
     * @author Zch
     * @date 17:14 2025/7/3 星期四
     * @return void
     **/
    @Override
    public void updateRepeat(UpdateRepeatDto dto) {
        UsePrint usePrint = this.getById(dto.getUsePrintId());
        if (ObjectUtil.isEmpty(usePrint))
            throw new XkdException("用印信息不存在");
        usePrint.setIsRepeat(dto.getIsRepeat());
        this.updateById(usePrint);
    }
}
